﻿

﻿using System.Threading;
using System.Web.Mvc;
using DotNetOpenAuth.Messaging;
using Microsoft.Web.WebPages.OAuth;
using System;
using System.Net.Http;
using System.Collections.Generic;
using Inovout.Runtime;
using Inovout.Web.Security;
using System.Linq;

namespace Inovout.Account.Web.Controllers
{
    public class OAuthdController : OAuthController
    {
        private static readonly string LocalAccessProviderName = "inovout";

        [AllowAnonymous]
        public string Providers()
        {
            //获取第三方登录服务集合
            List<AuthenticationClientData> authenticationClientDatas = new List<AuthenticationClientData>();
            InovoutOAuthClient inovoutOAuthClient = new InovoutOAuthClient(LocalAccessProviderName);
            AuthenticationClientData inovoutAuthClentData = new AuthenticationClientData(inovoutOAuthClient, "3cloud", new Dictionary<string, object>());
            authenticationClientDatas.Add(inovoutAuthClentData);
            authenticationClientDatas.AddRange(OAuthWebSecurity.RegisteredClientData.ToList());

            //变为指定字典
            List<Dictionary<string, string>> authenticationProvideData = new List<Dictionary<string, string>>();
            foreach (var authClientData in authenticationClientDatas)
            {
                Dictionary<string, string> clientData = new Dictionary<string, string>();
                clientData.Add("name", authClientData.DisplayName);
                clientData.Add("alais", authClientData.AuthenticationClient.ProviderName);
                clientData.Add("url", new Uri(this.Request.Url, Url.Action("Authorize", "OAuthd", new { provider = authClientData.AuthenticationClient.ProviderName, response_type = "code" })).AbsoluteUri);
                clientData.Add("logo_url", new Uri(this.Request.Url, Url.Content("~/Images/" + authClientData.AuthenticationClient.ProviderName + ".png")).AbsoluteUri);
                authenticationProvideData.Add(clientData);
            }

            //添加3coud登录服务
            return Newtonsoft.Json.JsonConvert.SerializeObject(authenticationProvideData);

        }

        private Uri GetRedirectUri(string returnUrl)
        {
            return new Uri(this.Request.Url, Url.Action("ExtensionalSignInCallback", "Account", new { returnUrl = returnUrl }));
        }

        [AllowAnonymous]
        public override ActionResult Authorize()
        {
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                var builder = new UriBuilder(new Uri(this.Request.Url, Url.Action("Authorize", "OAuth")));
                builder.Query = this.Request.Url.Query.TrimStart('?');
                return Redirect(builder.ToString());
            }
            else
            {
                //注册本地帐号和第三方帐号跳转的地址不同
                string returnUrl = this.Url.Content("~/OAuth/Authorize" + this.Request.Url.Query);
                bool skipBind;
                if(!bool.TryParse(this.Request.QueryString["skip_bind"],out skipBind))
                {
                    skipBind=false;
                }
                //根据privoder跳转至ExtensonalSignIn或SignIn
                if (this.Request.QueryString["provider"].Equals(LocalAccessProviderName))
                {
                    return RedirectToAction("SignIn", "Account", new { returnUrl = returnUrl,skipBind=skipBind });
                }
                else
                {
                    string provider = this.Request.QueryString["provider"].ToString();
                    return RedirectToAction("ExtensionalSignIn", "Account", new { provider = provider, returnUrl = returnUrl,skipBind=skipBind });
                }

            }
        }

        [HttpPost]
        public override ActionResult Token()
        {

            return base.Token();
        }
    }
}

